package com.itheima.pay.ali;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradeFastpayRefundQueryRequest;
import com.alipay.api.request.AlipayTradePrecreateRequest;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.request.AlipayTradeRefundRequest;
import com.alipay.api.response.AlipayTradeFastpayRefundQueryResponse;
import com.alipay.api.response.AlipayTradePrecreateResponse;
import com.alipay.api.response.AlipayTradeQueryResponse;
import com.alipay.api.response.AlipayTradeRefundResponse;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Description:
 * @Version: V1.0
 */
@RestController
@RequestMapping("alipay")
public class AliPayController {


    @GetMapping("precreate/{out_trade_no}")
    public AlipayTradePrecreateResponse precreate(@PathVariable("out_trade_no") String out_trade_no) {
        AlipayClient alipayClient = new DefaultAlipayClient(
                "https://openapi.alipaydev.com/gateway.do",
                "2021000119660082","MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCCZB89y75H5IB4YxWjBbtllswx6E9+VWYHc16GrkoMNtRVI0qagskVcoP0j8wAUDhM3mi9B8vFXKsnA3DxBxTrS4jrg5Y42yDn3HSHh34NHIvVS1/EGl4vmsH5M/bImbrxbwgiNtaCSujeEfMCrbod76ra+Wptyfy/rWtJsbdbexjJLUhR1geXQzCUr2s8IgDqLdzPRnNVgOCXbaVyE9Xv817ALbvH2DYqF4Q0vB5Ecg5K54silivyaZDTxwSR51cl+9a2CIRTiOhyMdtufGXxRr6GSEo4Z4oSuoGCdy/lYVSziIHwYvAP82X5oA7GFTQWO5OUnhKeGF8rG+vK5MmnAgMBAAECggEAcVEH4ZF+VUZc68LYm+uwV3I9FSZaZ2VaHSGDRbKUQZco4rCf9jWxAYWa7eBGHwOYWh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9+I5jtrezU2bKkrjouGNjlnx+FKwcuBsxuAsvJtcEF/zrTmhO9D3SBi7TgtUjcp+J64fnv2u","json","GBK",
                "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAgB536EHF79RM+Cj0z1HFeB2HRyQUCz5wPf21DX18NH7ByiA5m5ERo9411WuXIWtwy98JEgH0R3wDNCvsV7G0E1jHIa4OLkYioaSmi0vvRxO82PrzSEFh08txWKqF6jAuHTYOwuJdRQIt48fNEgacx41A5hR6wZSZXGjOo/ght4IUh8wMQc0WaJaSNkzKyqB9iqi/gy++JcbEU6z3t79VcOWBinehtnUSm5USDope+OCQ8hN2qQBl2vrU9cFMqK4e/B75CuoYmpVoRYK/JrwCmpeSMD5LAYE2+aKCURs81B9Sia8/Pa+kxfBRUGLnN1VIjzABT6YUfp4HCpjN0unoHQIDAQAB","RSA2");
        AlipayTradePrecreateRequest request = new AlipayTradePrecreateRequest();
        request.setNotifyUrl("");
        JSONObject bizContent = new JSONObject();
        bizContent.put("out_trade_no", out_trade_no); //20210817010101003
        bizContent.put("total_amount", 0.01);
        bizContent.put("subject", "黑马餐饮-消费");

        request.setBizContent(bizContent.toString());
        AlipayTradePrecreateResponse response = null;
        try {
            response = alipayClient.execute(request);
            if(response.isSuccess()){
                System.out.println(response.getQrCode());
                System.out.println("调用成功");
            } else {
                System.out.println("调用失败");
            }
            return response;
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }

        return null;
    }



    @GetMapping("query/{out_trade_no}")
    public AlipayTradeQueryResponse query(@PathVariable("out_trade_no") String out_trade_no) {
        AlipayClient alipayClient = new DefaultAlipayClient(
                "https://openapi.alipaydev.com/gateway.do",
                "2021000119660082","MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCCZB89y75H5IB4YxWjBbtllswx6E9+VWYHc16GrkoMNtRVI0qagskVcoP0j8wAUDhM3mi9B8vFXKsnA3DxBxTrS4jrg5Y42yDn3HSHh34NHIvVS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M7LhX/Rcf6bIszZR7QS7LWWvbAgvoDjdYq4XDwKBgGwh2UT/Fxaf6CbAX7svKMju99TdtipCAZkN+cbYko+ogWBcdfJUqLYQJGMwuLHu0RKuffYwotLQQASzqeI13Z6kklxQfcRjXMKDGCzorUPzmp+7uDqIQAvlTeavtEY2WrqO5F5qFTFhtbqxQVQA/PRRORLdkUPSEoynCwD1w25xAoGBALynDyzH//YgUdSHjgikOP5rQXrE7KemUTY/+SaTzK5ftYWqKKME2o/MJMyqeLeMP5oToi/HCLFYmcOlZJb7O/G2EPPQXyRSC91o9+I5jtrezU2bKkrjouGNjlnx+FKwcuBsxuAsvJtcEF/zrTmhO9D3SBi7TgtUjcp+J64fnv2u","json","GBK",
                "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAgB536EHF79RM+Cj0z1HFeB2HRyQUCz5wPf21DX18NH7ByiA5m5ERo9411WuXIWtwy98JEgH0R3wDNCvsV7G0E1jHIa4OLkYioaSmi0vvRxO82PrzSEFh08txWKqF6jAuHTYOwuJdRQIt48fNEgacx41A5hR6wZSZXGjOo/ght4IUh8wMQc0WaJaSNkzKyqB9iqi/gy++JcbEU6z3t79VcOWBinehtnUSm5USDope+OCQ8hN2qQBl2vrU9cFMqK4e/B75CuoYmpVoRYK/JrwCmpeSMD5LAYE2+aKCURs81B9Sia8/Pa+kxfBRUGLnN1VIjzABT6YUfp4HCpjN0unoHQIDAQAB","RSA2");

        AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();


        JSONObject bizContent = new JSONObject();
        bizContent.put("out_trade_no", out_trade_no); //20210817010101003

        request.setBizContent(bizContent.toString());
        try {
            AlipayTradeQueryResponse response = alipayClient.execute(request);
            if(response.isSuccess()){
                System.out.println(response);
                System.out.println("调用成功");
            } else {
                System.out.println("调用失败");
            }
            return response;
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }

        return null;
    }



    @GetMapping("refund/{out_trade_no}")
    public AlipayTradeRefundResponse refund(@PathVariable("out_trade_no") String out_trade_no) {
        AlipayClient alipayClient = new DefaultAlipayClient(
                "https://openapi.alipaydev.com/gateway.do",
                "2021000119660082","MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCCZB89y75H5IB4YxWjBbtllswx6E9+VWYHc16GrkoMNtRVI0qagskVcoP0j8wAUDhM3mi9B8vFXKsnA3DxBxTrS4jrg5Y42yDn3HSHh34NHIvVS1/EGl4vmsH5M/bImbrxbwgiNtaCSujeEfMCrbod76ra+Wptyfy/rWtJsbdbexjJLUhR1geXQzCUr2s8IgDqLdzPRnNVgOCXbaVyE9Xv817ALbvH2DYqF4Q0vB5Ecg5K54silivyaZDTxwSR51cl+9a2CIRTiOhyMdtufGXxRr6GSEo4Z4oSuoGCdy/lYVSziIHwYvAP82X5oA7GFTQWO5OUnhKeGF8rG+vK5MmnAgMBAAECggEAcVEH4ZF+VUZc68LYm+uwV3I9FSZaZ2VaHSGDRbKUQZco4rCf9jWxAYWa7eBGHwOYWh1zkswCqAuH5ojUD3PjFzV20Y5y8AhnyXjKgLocrtJfJ8ewe2Ga9u4cozEoyF7pm0auH+XeeC8Xv16D/ZDzTW7q4hCeELnYgM/6jHn46nN6UABpht38/fcTRqau/SHqqK5sKhWxJSx7ce0tiEd8mQh0Tnh3z2wfhlpU6gUeP91Wv75mHDDvEzxwLswIpPt76jwc9J3Xsc4f2/balNFhIssP6v7GC+MSR//g8Jzz2qK//IkGkiBixtGNuTvzIYTD5bvB/5aqeazXX7Z6KXC1YQKBgQD90CrOqlqq9yMf8ynASPwYEqdahwla4hg4rBbCAm5TqIXPmAtOGTPEt5O7RMI2EzTDjkYAhkQIXociatkPgePdqHGzBmpFTP2ipelkG2QPUdf7mzBXQ6gFrBW2DrfYoGGjSBq7QY98tmAIBBcY8BeO4Q7d1nfLkk7CEal/o3F0VwKBgQCDg7llkf/AaOSkl1rqQnIC9DAxIZa2GekWPwnQfjo8s7hrDhjl9DQtZzvJUDopwfpCMkbRzP13Fdpn/qm88fhL6P3aIVUYx/Wm67Pt5M6R/31ZJsf0j+zdPpAmhw/c2yvLAdLwh1JTOGcYdRSLP8jqTb16mVRvIQGHScsfemODMQKBgQDQSMToRcoT5K0xaeXIQtyFkME/O/OEXmgMEcS3PzF0e6YbnqUgWPQvqYqVMi7CG27AT//ILGamC5SPvOkVnbz1yGZNyKTq6+x0TfzCRuwM2lgIxOidpAq9ZOOIPHnITozxZplxM7LhX/Rcf6bIszZR7QS7LWWvbAgvoDjdYq4XDwKBgGwh2UT/Fxaf6CbAX7svKMju99TdtipCAZkN+cbYko+ogWBcdfJUqLYQJGMwuLHu0RKuffYwotLQQASzqeI13Z6kklxQfcRjXMKDGCzorUPzmp+7uDqIQAvlTeavtEY2WrqO5F5qFTFhtbqxQVQA/PRRORLdkUPSEoynCwD1w25xAoGBALynDyzH//YgUdSHjgikOP5rQXrE7KemUTY/+SaTzK5ftYWqKKME2o/MJMyqeLeMP5oToi/HCLFYmcOlZJb7O/G2EPPQXyRSC91o9+I5jtrezU2bKkrjouGNjlnx+FKwcuBsxuAsvJtcEF/zrTmhO9D3SBi7TgtUjcp+J64fnv2u","json","GBK",
                "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAgB536EHF79RM+Cj0z1HFeB2HRyQUCz5wPf21DX18NH7ByiA5m5ERo9411WuXIWtwy98JEgH0R3wDNCvsV7G0E1jHIa4OLkYioaSmi0vvRxO82PrzSEFh08txWKqF6jAuHTYOwuJdRQIt48fNEgacx41A5hR6wZSZXGjOo/ght4IUh8wMQc0WaJaSNkzKyqB9iqi/gy++JcbEU6z3t79VcOWBinehtnUSm5USDope+OCQ8hN2qQBl2vrU9cFMqK4e/B75CuoYmpVoRYK/JrwCmpeSMD5LAYE2+aKCURs81B9Sia8/Pa+kxfBRUGLnN1VIjzABT6YUfp4HCpjN0unoHQIDAQAB","RSA2");

        AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();


        JSONObject bizContent = new JSONObject();
        bizContent.put("out_trade_no", out_trade_no); //20210817010101003
        bizContent.put("refund_amount", "0.01");
        bizContent.put("refund_reason", "正常退款");

        request.setBizContent(bizContent.toString());
        try {
            AlipayTradeRefundResponse response = alipayClient.execute(request);
            if(response.isSuccess()){
                System.out.println(response);
                System.out.println("调用成功");
            } else {
                System.out.println("调用失败");
            }
            return response;
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }

        return null;
    }


    @GetMapping("refundquery/{out_trade_no}/{out_request_no}")
    public AlipayTradeFastpayRefundQueryResponse refundQuery(@PathVariable("out_trade_no") String out_trade_no,
                                                 @PathVariable("out_request_no") String out_request_no) {
        AlipayClient alipayClient = new DefaultAlipayClient(
                "https://openapi.alipaydev.com/gateway.do",
                "2021000119660082","MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCCZB89y75H5IB4YxWjBbtllswx6E9+VWYHc16GrkoMNtRVI0qagskVcoP0j8wAUDhM3mi9B8vFXKsnA3DxBxTrS4jrg5Y42yDn3HSHh34NHIvVS1/EGl4vmsH5M/bImbrxbwgiNtaCSujeEfMCrbod76ra+Wptyfy/rWtJsbdbexjJLUhR1geXQzCUr2s8IgDqLdzPRnNVgOCXbaVyE9Xv817ALbvH2DYqF4Q0vB5Ecg5K54silivyaZDTxwSR51cl+9a2CIRTiOhyMdtufGXxRr6GSEo4Z4oSuoGCdy/lYVSziIHwYvAP82X5oA7GFTQWO5OUnhKeGF8rG+vK5MmnAgMBAAECggEAcVEH4ZF+VUZc68LYm+uwV3I9FSZaZ2VaHSGDRbKUQZco4rCf9jWxAYWa7eBGHwOYWh1zkswCqAuH5ojUD3PjFzV20Y5y8AhnyXjKgLocrtJfJ8ewe2Ga9u4cozEoyF7pm0auH+XeeC8Xv16D/ZDzTW7q4hCeELnYgM/6jHn46nN6UABpht38/fcTRqau/SHqqK5sKhWxJSx7ce0tiEd8mQh0Tnh3z2wfhlpU6gUeP91Wv75mHDDvEzxwLswIpPt76jwc9J3Xsc4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CvsV7G0E1jHIa4OLkYioaSmi0vvRxO82PrzSEFh08txWKqF6jAuHTYOwuJdRQIt48fNEgacx41A5hR6wZSZXGjOo/ght4IUh8wMQc0WaJaSNkzKyqB9iqi/gy++JcbEU6z3t79VcOWBinehtnUSm5USDope+OCQ8hN2qQBl2vrU9cFMqK4e/B75CuoYmpVoRYK/JrwCmpeSMD5LAYE2+aKCURs81B9Sia8/Pa+kxfBRUGLnN1VIjzABT6YUfp4HCpjN0unoHQIDAQAB","RSA2");

        AlipayTradeFastpayRefundQueryRequest request = new AlipayTradeFastpayRefundQueryRequest();

        JSONObject bizContent = new JSONObject();
        bizContent.put("out_trade_no", out_trade_no); //20210817010101003
        bizContent.put("out_request_no", out_request_no);

        request.setBizContent(bizContent.toString());
        try {
            AlipayTradeFastpayRefundQueryResponse response = alipayClient.execute(request);
            if(response.isSuccess()){
                System.out.println(response);
                System.out.println("调用成功");
            } else {
                System.out.println("调用失败");
            }
            return response;
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }

        return null;
    }
}
